#include <boost/test/unit_test.hpp>
#include <qua/field.h>
#include <qua/text.h>

using namespace qua;

struct field_test_fixture
{
    char field_format_data_buffer[ 0x100 ];
    char field_data_buffer[ 0x100 ];
    char field_format_buffer[ sizeof(field::format) ];
    char field_buffer[ sizeof(field) ];

    char*           field_format_data_ptr;
    char*           field_data_ptr;
    field::format*  field_format_ptr;
    field*          field_ptr;

    field_test_fixture()
        : field_format_data_ptr( field_format_data_buffer ),
          field_data_ptr( field_data_buffer ),
          field_format_ptr( nullptr ),
          field_ptr( nullptr )
    {
        memset( field_format_data_buffer, 0, sizeof(field_format_data_buffer) );
        memset( field_data_buffer, 0, sizeof(field_data_buffer) );
        memset( field_format_buffer, 0, sizeof(field_format_buffer) );
        memset( field_buffer, 0, sizeof(field_buffer) );
    }

    ~field_test_fixture()
    {
        if( field_ptr ) field_ptr->~field();
        if( field_format_ptr ) field_format_ptr->~format();
    }
};

BOOST_FIXTURE_TEST_SUITE( field_test_suite, field_test_fixture )

BOOST_AUTO_TEST_CASE( field_simple_test_case )
{
    BOOST_REQUIRE_NO_THROW( ( field_format_ptr = new(field_format_buffer) field::format( field_format_data_ptr, L"first", field::of_int ) ) );
    BOOST_REQUIRE_NO_THROW( ( field_ptr = new(field_buffer) field( field_data_ptr, *field_format_ptr ) ) );

    BOOST_CHECK_EQUAL( field_format_ptr->get_name(), L"first" );
    BOOST_CHECK_EQUAL( field_format_ptr->get_type(), field::of_int );
    BOOST_CHECK( field_format_ptr->is_null_default() );

    BOOST_CHECK_EQUAL( field_ptr->get_name(), L"first" );
    BOOST_CHECK_EQUAL( field_ptr->get_type(), field::of_int );
    BOOST_CHECK_EQUAL( &field_ptr->get_format(), field_format_ptr );

    BOOST_REQUIRE_NO_THROW( field_format_ptr->set_default( 123 ) );
    BOOST_REQUIRE_NO_THROW( field_ptr->set_value( 234 ) );

    BOOST_CHECK_EQUAL( field_format_ptr->get_default<int>(), 123 );
    BOOST_CHECK_EQUAL( field_ptr->get_value<int>(), 234 );
}

BOOST_AUTO_TEST_SUITE_END()
